// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 * Creator: yanking
 * Create time: 2022-02-20 20:34
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.leetCode.EatBanana;

public class EatBanana {
    public int minEatingSpeed(int[] piles, int h) {
        // 对香蕉堆进行排序
        // 使用二分查找实现最快吃香蕉速度的获取，最快吃香蕉的速度为堆中香蕉的最大的量，最慢为堆中香蕉最小的量
        int len = piles.length;
        int max = 1;
        for (int pile : piles) {
            max = Math.max(max, pile);
        }
        int min = 1;
        while (max > min) {
            int e = (max + min) >> 1;
            int hour = countEatTime(piles, e);
            if (hour > h) {
                // 吃的速度太慢
                min = e + 1;
            } else {
                // 吃的速度太快
                max = e;
            }
        }

        return min;

    }

    public int countEatTime(int[] piles, int k) {
        // 根据给定的香蕉堆和每小时吃香蕉的数量计算相应的吃香蕉时间
        int hour = 0;
        for (int i = 0; i < piles.length; ++i) {
            hour += Math.ceil(piles[i] / (double) k);
        }
        return hour;
    }

    public int getMin(int[] piles, int k, int h) {
        if (countEatTime(piles, k - 1) == h) {
            return getMin(piles, k - 1, h);
        } else {
            return k;
        }
    }

}


